<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #app {
            width: 500px;
            margin: 100px auto;
            background-color: antiquewhite;
        }

        h1 {
            text-align: center;
        }

        table {
            border: 1px solid #ccc;
            width: 100%;
        }

        th,
        td {
            height: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid #ccc;
        }

        .grid .total {
            height: 30px;
            line-height: 30px;
            background-color: antiquewhite;
            border-top: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="grid">
            <div>
                <h1>图书管理</h1>
                <div class="book">
                    <div>
                        <label for="">
                            编号：<input type="text" id="id" v-model="id" :disabled="flag">
                        </label>
                        <label for="">
                            名称：<input type="text" id="name" v-model="name">
                        </label>
                        <button @click="handle" :disabled="submitFlag">提交</button>
                    </div>
                </div>
            </div>
            <div class="total">
                <span>图书总数:</span>
                <span>{{total}}</span>
            </div>
            <table cellspacing="0">
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in books" :key="item.id">
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.data}}</td>
                        <td>
                            <a href="" @click.prevent="edit(item.id)">修改</a>
                            <span>|</span>
                            <a href="" @click.prevent="remove(item.id)">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <script src="./public/js/vue.min.js"></script>
    <script src="./public/js/axios.min.js"></script>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                flag: false,
                submitFlag: false,
                id: '',
                name: '',
                books: {},
            },
            methods: {
                handle() {
                    if (this.flag) {
                        this.books.some((item) => {
                            item.name = this.name;
                            return true;
                        })
                        this.id = '';
                        this.name = '';
                        this.flag = false;
                    } else {
                        var book = {};
                        book.id = this.id;
                        book.name = this.name;
                        book.data = this.data;
                        this.books.push(book);

                        this.id = '';
                        this.name = '';
                    }
                },
                edit(id) {
                    var book = this.books.filter((item) => {
                        return item.id == id
                    });
                    // console.log(book);
                    this.id = book[0].id;
                    this.name = book[0].name;
                    this.flag = true;
                },
                remove(id) {
                    var index = this.books.findIndex((item) => {
                        return item.id == id;
                    });
                    this.books.splice(index,1);
                },
            },
            computed: {
                total() {
                    return this.books.length;
                }
            },
            watch: {
                name(val) {
                    var flag = this.books.some((item) => {
                        return item.name == val
                    })
                    if(flag) {
                        this.submitFlag = true;
                    }else {
                        this.submitFlag = false
                    }
                }
            },
            mounted() {
                var data = [{
                    "id": "4",
                    "name": "红楼梦",
                    "data": "25525609975000"
                },
                {
                    "id": "5",
                    "name": "三国演义",
                    "data": "25525609975000"
                },
                {
                    "id": "6",
                    "name": "西游记",
                    "data": "25525609975000"
                },
                {
                    "id": "7",
                    "name": "水浒传",
                    "data": "25525609975000"
                }];
                this.books = data;
            }
        })
    </script>
</body>

</html>